package logic

import (
	"context"
	"weee/common"
	"weee/models"
	"weee/orderrpc/orderrpcclient"

	"weee/api/internal/svc"
	"weee/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AliPayLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAliPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AliPayLogic {
	return &AliPayLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AliPayLogic) AliPay(req *types.AliPayRequest) (resp *types.AliPayResponse, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.OrderRpc.AliPay(context.Background(), &orderrpcclient.AliPayRequest{
		OrderId: int32(req.OrderId),
	})
	if res.Code != 200 {
		return &types.AliPayResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
		}, nil
	}
	order := models.Order3{
		OrderSn: res.OrderSn,
		Price:   float64(res.Money),
	}
	ali := new(common.AliPay)
	url, err := ali.Payment(order)
	if err != nil {
		return &types.AliPayResponse{
			Code: 400,
			Msg:  "支付宝支付失败",
		}, nil
	}
	return &types.AliPayResponse{
		Code: 200,
		Msg:  "支付宝支付成功",
		Url:  url,
	}, nil
}
